﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using PurchaseOrderExtra;
using MyException;
using System.Transactions;

namespace BusinessLogicLayer
{
    public class ReceiveDeliveryControl
    {
        PurchaseOrderDetailRepository purchaseOrderDetailRepository;
        PurchaseOrderRepository purchaseOrderRepo;
        StationeryRepository stationeryRepo;
        StockHistoryRepository stockHistoryRepo;
        UpdateStockDAL updateStockDAL;

        public ReceiveDeliveryControl()
        {
            purchaseOrderDetailRepository = new PurchaseOrderDetailRepository();
            purchaseOrderRepo = new PurchaseOrderRepository();
            stationeryRepo = new StationeryRepository();
            stockHistoryRepo = new StockHistoryRepository();
            updateStockDAL = new UpdateStockDAL();
        }

        public List<DeliveryDisplay> GetDeliveryDisplayByPOId(int poId)
        {
            PurchaseOrder purchaseOrder = purchaseOrderRepo.GetPurchaseOrderByPOId(poId);
            string status = purchaseOrder.Status;

            List<DeliveryDisplay> list = new List<DeliveryDisplay>();
            List<PurchaseOrderDetail> poDetailList = new List<PurchaseOrderDetail>();
            poDetailList = purchaseOrderDetailRepository.GetPurchaseOrderDetailByPOId(poId);

            foreach (PurchaseOrderDetail poDetail in poDetailList)
            {
                DeliveryDisplay deliveryDisplay = new DeliveryDisplay();
                deliveryDisplay.POId = (int)poDetail.POID;
                deliveryDisplay.ItemCode = poDetail.ItemCode;
                deliveryDisplay.ItemName = poDetail.Stationery.ItemName;
                deliveryDisplay.Quantity = (int)poDetail.POQty;
                deliveryDisplay.Status = status;
                list.Add(deliveryDisplay);
            }
            return list;
        }

        public PurchaseOrder GetPurchaseOrderByPOId(int poId)
        {
            return purchaseOrderRepo.GetPurchaseOrderByPOId(poId);
        }

        public void UpdateDeliveryOrderInfo(int poId, string deliveryOrderNo, DateTime deliveryDate)
        {
            try
            {
                PurchaseOrder purchaseOrder = purchaseOrderRepo.GetPurchaseOrderByPOId(poId);
                if (deliveryDate.Date < purchaseOrder.RequestedDate.Value.Date)
                {
                    throw new DeliveryDateBeforeRequestedDateException();
                }
                string status = purchaseOrder.Status;
                if (status == "Approved")
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        // Update purchase order table
                        purchaseOrder.DeliveryOrderNo = deliveryOrderNo;
                        purchaseOrder.DeliveryDate = deliveryDate;
                        purchaseOrder.Status = "Delivered";
                        purchaseOrderRepo.UpdatePurchaseOrder(purchaseOrder);

                        List<PurchaseOrderDetail> poDetailList = purchaseOrderDetailRepository.GetPurchaseOrderDetailByPOId(poId);
                        foreach (PurchaseOrderDetail poDetail in poDetailList)
                        {
                            string itemCode = poDetail.ItemCode; 
                            
                            Stationery stationery = stationeryRepo.GetStationeryByItemCode(itemCode);
                            int currentQuantity = stationery.AvailableQty;
                            int adjustQuantity = (int)poDetail.POQty;

                            // Update outstanding table
                            updateStockDAL.updateOutstandingTable(itemCode, adjustQuantity);

                            // Update stationery table
                            stationery.AvailableQty = currentQuantity + adjustQuantity;
                            stationeryRepo.UpdateStationery(stationery);                               

                            // Update stock history table
                            StockHistory stockHistory = new StockHistory();
                            stockHistory.ItemCode = itemCode;
                            stockHistory.Date = deliveryDate;
                            stockHistory.Description = "Supplier - " + purchaseOrder.Supplier.SupplierName;
                            stockHistory.AdjustedQty = adjustQuantity;
                            //stockHistory.Balance = stationery.AvailableQty;
                            stockHistory.Balance = currentQuantity + adjustQuantity;
                            stockHistoryRepo.CreateStockHistory(stockHistory);
                        }
                        ts.Complete();
                    }
                }
                else if (status == "Pending")
                {
                    throw new PendingPurchaseOrderException();
                }
                else if (status == "Rejected")
                {
                    throw new RejectedPurchaseOrderException();
                }
                else if (status == "Delivered")
                {
                    throw new DeliveredPurchaseOrderException();
                }
            }
            catch (DeliveryDateBeforeRequestedDateException ex)
            {
                throw new DeliveryDateBeforeRequestedDateException();
            }
            catch (Exception ex)
            {
            }
        }

    }
}
    

